var superagent = require('superagent');
var charset = require('superagent-charset');
charset(superagent);
var baseUrl = 'http://sina.cn'; //输入任何网址都可以
const cheerio = require('cheerio');
var app = require('express')();
app.set("view engine", "ejs");
app.get('/', function (req, res) {
    //设置请求头
    res.header("Access-Control-Allow-Origin", "*");
    res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header('Access-Control-Allow-Headers', 'Content-Type');

    var reqURL = baseUrl;
    //网页页面信息是gb2312，所以chaeset应该为.charset('gb2312')，一般网页则为utf-8,可以直接使用.charset('utf-8')
    superagent.get(reqURL).charset('utf-8').end(function (err, sres) {
        let items = [];
        if (err) {
            console.log('ERR: ' + err);
            res.json({
                code: 400,
                msg: err,
                sets: items
            });
            return;
        }
        var $ = cheerio.load(sres.text);
        $('img').each(function (idx, element) {
            var $element = $(element);
            let imgSrc = $element.attr('data-src');
            if (imgSrc) {
                items.push({
                    imgSrc
                });
            }
        });
        var eJsData = {
            items: items
        }
        // 渲染ejs
        res.render('content', eJsData);
        // res.jsonp({
        //     code: 200,
        //     msg: "请求成功",
        //     data: items
        // });
    });
});
var server = app.listen(3000, function () {

    var host = server.address().address
    var port = server.address().port

    console.log("应用实例，访问地址为 http://%s:%s", host, port)

})